package cn.jekun.web.shop.service;

import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.NotNull;
import cn.jekun.component.base.validator.annotation.Num;
import cn.jekun.component.base.validator.annotation.Security;
import cn.jekun.component.core.bean.gen.ErpInventory;
import cn.jekun.component.core.bean.gen.ErpMerchandise;
import cn.jekun.component.core.bean.gen.ErpMerchandiseImage;
import cn.jekun.component.core.bean.gen.ErpMerchandiseType;
import cn.jekun.component.core.bean.gen.ErpPublishTask;
import cn.jekun.component.core.bean.gen.ErpSalePrice;
import cn.jekun.component.core.service.gen.ErpInventoryService;
import cn.jekun.component.core.service.gen.ErpMerchandiseImageService;
import cn.jekun.component.core.service.gen.ErpMerchandiseService;
import cn.jekun.component.core.service.gen.ErpMerchandiseTypeService;
import cn.jekun.component.core.service.gen.ErpPublishTaskService;
import cn.jekun.component.core.service.gen.ErpSalePriceService;
import cn.jekun.web.shop.bean.Merchandise;
import cn.jekun.web.shop.bean.MerchandiseType;

@Service(MerchandiseService.BEAN_NAME)
public class MerchandiseService extends ShopBaseService {

	public static final String BEAN_NAME="cn.jekun.web.shop.service.MerchandiseService";
	public static final String NAMESPACE="ACTION_ERP_MERCHANDISE";

	@Override
	public String getNamespace() {
		return NAMESPACE;
	}
	@Resource(name=ErpMerchandiseTypeService.BEAN_NAME)
	ErpMerchandiseTypeService erpMerchandiseTypeService;
	@Resource(name=ErpMerchandiseImageService.BEAN_NAME)
	ErpMerchandiseImageService erpMerchandiseImageService;
	
	@Resource(name=ErpMerchandiseService.BEAN_NAME)
	ErpMerchandiseService erpMerchandiseService;

	@Resource(name = ErpPublishTaskService.BEAN_NAME)
	protected ErpPublishTaskService erpPublishTaskService;

	@Resource(name = ErpSalePriceService.BEAN_NAME)
	ErpSalePriceService erpSalePriceService;
	
	@Resource(name = ErpInventoryService.BEAN_NAME)
	ErpInventoryService erpInventoryService;
	
	/**
	 * 商品列表
	 * @param shopId
	 * @param merStatus
	 * @param keywordsType
	 * @param keywords
	 * @param page
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Merchandise> getQueryMerchandList(
			@ErpMerchandise.AnnoShopId String shopId,
			@Security String merStatus,
			@Security String keywordsType,
			@Security String keywords,
			Page page,
			@Security String beginDate,
			@Security String endDate, 
			@Security String beginPrice, 
			@Security String endPrice,
			@Security String beginInventory, 
			@Security String endInventory, 
			@Security String sysType,
			@Security String perType, 
			@Security String seoWeight,
			@Security String sort,
			@Security String isWarmMer){
		
		Map formMap=new HashMap<String, String>();
		String SQL="";
		String orderby = "";
		if(StringUtils.isNotBlank(sort)){
			switch (Integer.parseInt(sort)) {
			case 1:
				orderby += "case when a.update_time is null then a.create_time else a.update_time end desc nulls last";
				break;
			case 2:
				orderby += "case when a.update_time is null then a.create_time else a.update_time end asc";
				break;
			case 3:
				orderby += "case when a.seo_weight is null then 0 else a.seo_weight end desc nulls last";
				break;
			case 4:
				orderby += "case when a.seo_weight is null then 0 else a.seo_weight end asc";
				break;
			case 5:
				orderby += "i.inventory desc nulls last";
				break;
			case 6:
				orderby += "i.inventory asc";
				break;
			case 7:
				orderby += "case when s.sales is null then 0 else s.sales end desc nulls last";
				break;
			case 8:
				orderby += "case when s.sales is null then 0 else s.sales end asc";
				break;
			case 9:
				orderby += "p.price desc nulls last";
				break;
			case 10:
				orderby += "p.price asc";
				break;
			default:
				//orderby += "case when a.update_time is null then a.create_time else a.update_time end desc nulls last";
				break;
			}
		
		}
		if(merStatus!= null&&merStatus!=""){
			if("shelves".equals(merStatus)){//上架
				formMap.put(Merchandise.COL_CLOSED, '1');
			}else if("soldOut".equals(merStatus)){//下架
				formMap.put(Merchandise.COL_CLOSED, '0');
			}else if("recom".equals(merStatus)){//推荐
				formMap.put(Merchandise.COL_RECOMMENDED, '1');
			}else if("unrecom".equals(merStatus)){//非推荐
				formMap.put(Merchandise.COL_RECOMMENDED, '0');
			}
		}
		if(keywordsType!=null&&keywordsType!=""){//关键字类型
			formMap.put(keywordsType,keywords);
		
		}
		formMap.put("UPDATE_TIME_BEGIN", beginDate);
		formMap.put("UPDATE_TIME_END", endDate);
		formMap.put("PRICE_BEGIN", beginPrice);
		formMap.put("PRICE_END", endPrice);
		formMap.put("INVENTORY_BEGIN", beginInventory);
		formMap.put("INVENTORY_END", endInventory);
		formMap.put(ErpMerchandise.COL_SEO_WEIGHT, seoWeight);
		formMap.put(Merchandise.COL_SHOP_ID, shopId);
		
		if(sysType!=null && sysType.toString().length()>0){
			SQL +=" and a.MERCHANDISE_TYPE_ID in (select MERCHANDISE_TYPE_ID from erp_merchandise_type where MERCHANDISE_TYPE_ID = '"+sysType+"' or parent_id='"+sysType+"')"; 
		}

		if(perType!=null && perType.toString().length()>0){
			SQL +=" and a.SHOP_MERCHANDISE_TYPE_ID in (select MERCHANDISE_TYPE_ID from erp_merchandise_type where MERCHANDISE_TYPE_ID = '"+perType+"' or parent_id='"+perType+"')"; 
		}
		if(isWarmMer!=null && "showWarmMer".equals(isWarmMer.toString())){//选择“只看缺货商品”
			SQL += " and i.inventory<=i.warn";
		}
		if("image".equals(merStatus)){//有图
			SQL += " and exists (select 1 from erp_merchandise_image mi where a.merchandise_id = mi.goods_id)";
		}else if("unimage".equals(merStatus)){//无图
			SQL += " and not exists (select 1 from erp_merchandise_image mi where a.merchandise_id = mi.goods_id)";
		}
		
		SqlUtis.convertBetweenValue(formMap, ErpMerchandise.COL_UPDATE_TIME);
		SqlUtis.convertBetweenValue(formMap, ErpSalePrice.COL_PRICE);
		SqlUtis.convertBetweenValue(formMap, ErpInventory.COL_INVENTORY);
		SqlUtis.prepareSql(formMap
				, SqlUtis.getSQL(formMap,"a."+Merchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),//商家ID
				SqlUtis.getSQL(formMap,"a."+Merchandise.COL_RECOMMENDED,Types.VARCHAR,SqlUtis.EQ),//推荐
				SqlUtis.getSQL(formMap,"a."+Merchandise.COL_CLOSED,Types.VARCHAR,SqlUtis.EQ),//上架下架
				SqlUtis.getSQL(formMap,"a."+Merchandise.COL_MERCHANDISE_NAME,Types.VARCHAR,SqlUtis.LIKE),//商品名称
				SqlUtis.getSQL(formMap,"a."+Merchandise.COL_BARCODE,Types.VARCHAR,SqlUtis.LIKE),//条形码

				SqlUtis.getSQL(formMap,"a."+ErpMerchandise.COL_SEO_WEIGHT,Types.VARCHAR,SqlUtis.EQ),//条形码
				SqlUtis.getSQL(formMap, "a." + ErpMerchandise.COL_UPDATE_TIME, Types.DATE),
				SqlUtis.getSQL(formMap, "p." + ErpSalePrice.COL_PRICE, Types.VARCHAR),
				SqlUtis.getSQL(formMap, "i." + ErpInventory.COL_INVENTORY, Types.VARCHAR),SQL,
				StringUtils.isNotBlank(orderby)?SqlUtis.orderBy(orderby):"");
		
		return this.list(formMap,page);
		
	}
	
	
	/**
	 * 修改商品信息
	 * @param mapInfoMap
	 */
	@Transactional
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Integer modifyGoodsInfo(@ErpMerchandise.AnnoMerchandiseId String merchandiseId,@ErpMerchandise.AnnoBarcode String barcode,@ErpMerchandise.AnnoShopId String shopId,@ErpMerchandise.AnnoMerchandiseName String merchandiseName,
			@ErpMerchandise.AnnoBrief String brief,@ErpMerchandise.AnnoMerchandiseTypeId String merchandiseTypeId,@ErpMerchandise.AnnoShopMerchandiseTypeId String shopMerchandiseTypeId,@ErpMerchandise.AnnoUnitName String unitName,@Num(length=5) String inventory,
			@Num(length=5,scale=2)String costPrice,@Num(length=5,scale=2)String mktprice,@Num(length=5,scale=2) String salePrice,@ErpMerchandise.AnnoVendorName String vendorName,String detail,
			@ErpMerchandise.AnnoClosed String closed,@ErpMerchandise.AnnoRecommended String recommended,@Num(length=5)String inventoryWarn,Map mapInfoMap,@ErpMerchandise.AnnoSeoWeight String seoWeight) {
		
		Merchandise merchandise=new Merchandise();
		merchandise.setMerchandiseId(merchandiseId);
		merchandise.setBarcode(barcode);
		merchandise.setShopId(shopId);
		merchandise.setMerchandiseName(merchandiseName);
		merchandise.setBrief(brief);
		merchandise.setMerchandiseTypeId(merchandiseTypeId);
		merchandise.setShopMerchandiseTypeId(shopMerchandiseTypeId);
		merchandise.setUnitName(unitName);
		if(StringUtils.isNotBlank(seoWeight)){
			merchandise.setSeoWeight(Double.parseDouble(seoWeight));
		}else{
			merchandise.setSeoWeight(0.0);
		}
		if(!(inventory==null || "".equals(inventory))){
			merchandise.setInventory(Integer.parseInt(inventory));
		}else{
			merchandise.setInventory(0);
		}
		if(!(costPrice==null || "".equals(costPrice))){
			merchandise.setCostPrice(Double.parseDouble(costPrice));
		}else{
			merchandise.setCostPrice(0.0);
		}
		if(!(mktprice==null || "".equals(mktprice))){
			merchandise.setMktprice(Double.parseDouble(mktprice));
		}else{
			merchandise.setMktprice(0.0);
		}
		if(!(salePrice==null || "".equals(salePrice))){
			merchandise.setSalePrice(Double.parseDouble(salePrice));
		}else{
			merchandise.setSalePrice(0.0);
		}
		if(!(inventoryWarn==null || "".equals(inventoryWarn))){
			merchandise.setInventoryWarn(Integer.parseInt(inventoryWarn));
		}else{
			merchandise.setInventoryWarn(0);
		}
		merchandise.setVendorName(vendorName);
		merchandise.setDetail(detail);
		merchandise.setClosed(closed);
		merchandise.setRecommended(recommended);
		
		mapInfoMap.put("merchandise", merchandise);
		mapInfoMap.put("shopId", shopId);
		
		
		
		List<ErpMerchandiseImage> goodsPicList = (List<ErpMerchandiseImage>) mapInfoMap.get("goodsPicList");
		ErpMerchandiseImageService erpMerchandiseImageService = (ErpMerchandiseImageService) mapInfoMap.get("erpMerchandiseImageService");
		ErpInventory erpInventory = (ErpInventory) mapInfoMap.get("erpInventory");
		ErpInventoryService erpInventoryService = (ErpInventoryService) mapInfoMap.get("erpInventoryService");
		ErpSalePrice erpSalePrice = (ErpSalePrice) mapInfoMap.get("erpSalePrice");
		ErpSalePriceService erpSalePriceService= (ErpSalePriceService) mapInfoMap.get("erpSalePriceService");
		String defImgPath = (String) mapInfoMap.get("defImgPath");
		Integer resultInfo = new Integer(0); 
		//修改商品基本信息
		Map goodsInfoMap = new HashMap();
		goodsInfoMap.put("erpMerchandise", merchandise);
		goodsInfoMap.put(Merchandise.COL_MERCHANDISE_ID, merchandise.getMerchandiseId());
		goodsInfoMap.put(Merchandise.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(goodsInfoMap,
				SqlUtis.getSQL(goodsInfoMap,"a."+Merchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),
				SqlUtis.getSQL(goodsInfoMap,"a."+Merchandise.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		resultInfo = this.erpMerchandiseService.update(goodsInfoMap);
		//修改库存和警告阀值
		erpInventory =new ErpInventory();
		erpInventory.setInventory(merchandise.getInventory());
		erpInventory.setWarn(merchandise.getInventoryWarn());
		Map InvMap = new HashMap();
		InvMap.put("erpInventory", erpInventory);
		InvMap.put(ErpInventory.COL_MERCHANDISE_ID, merchandise.getMerchandiseId());
		SqlUtis.prepareSql(InvMap,
				SqlUtis.getSQL(InvMap,"a."+ErpInventory.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		resultInfo = erpInventoryService.update(InvMap);
		//修改商品销售价和商品成本价
		erpSalePrice = new ErpSalePrice();
		erpSalePrice.setPrice(merchandise.getSalePrice());
		erpSalePrice.setCost(merchandise.getCostPrice());
		Map priceMap = new HashMap();
		priceMap.put("erpSalePrice", erpSalePrice);
		priceMap.put(ErpSalePrice.COL_MERCHANDISE_ID,merchandise.getMerchandiseId());
		SqlUtis.prepareSql(priceMap,
				SqlUtis.getSQL(priceMap,"a."+ErpSalePrice.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		resultInfo =erpSalePriceService.update(priceMap);
		//新增商品图片 
		if(goodsPicList!=null){
			for (ErpMerchandiseImage erpMerchandiseImage : goodsPicList) {
				
				String imageuRL=erpMerchandiseImage.getImageUrl();
				
				Map picMap = new HashMap();
				
				picMap.put("image_url", imageuRL);
				
				SqlUtis.prepareSql(picMap, SqlUtis.getSQL(picMap, "a.image_url", Types.VARCHAR, SqlUtis.EQ));
				
				boolean exists = erpMerchandiseImageService.exists(picMap);
				
				if(!exists){
					picMap = new HashMap();
					erpMerchandiseImage.setGoodsId(merchandise.getMerchandiseId());
					picMap.put("erpMerchandiseImage", erpMerchandiseImage);
					picMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandise.getMerchandiseId());
					SqlUtis.prepareSql(picMap, 
							SqlUtis.getSQL(picMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ)
							);
					String key =erpMerchandiseImageService.insert(picMap).toString();
					if(key==null){
						resultInfo = 0;
					}
				}
				
			
			}
		}
		//设置默认图片
		if(defImgPath!=null){
			Map setMap = new HashMap();
			setMap.put(ErpMerchandiseImage.COL_DEF, "1");
			setMap.put(ErpMerchandiseImage.COL_IMAGE_URL, defImgPath);
			setMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandise.getMerchandiseId());
			SqlUtis.prepareSql(setMap, 
					SqlUtis.getSQL(setMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ),
					SqlUtis.getSQL(setMap,"a."+ErpMerchandiseImage.COL_IMAGE_URL,Types.VARCHAR,SqlUtis.EQ)
					);
			List<ErpMerchandiseImage> list = this.erpMerchandiseImageService.list(setMap);
			ErpMerchandiseImage merchandiseImage = new ErpMerchandiseImage();
			SqlUtis.prepareSql(setMap, 
					SqlUtis.getSQL(setMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ),
					SqlUtis.getSQL(setMap,"a."+ErpMerchandiseImage.COL_DEF,Types.VARCHAR,SqlUtis.EQ)
					);
			merchandiseImage.setDef("0");
			setMap.put("erpMerchandiseImage", merchandiseImage);
			erpMerchandiseImageService.update(setMap);

			//如果同一商品出现相同图片不止一张
			if(list.size()>1){
				ErpMerchandiseImage defImage1 = new ErpMerchandiseImage();
				defImage1.setDef("1");
				defImage1.setImageId(list.get(0).getImageId());
				erpMerchandiseImageService.update(defImage1);
			}else{
			Map defImgMap = new HashMap();
			ErpMerchandiseImage defImage = new ErpMerchandiseImage();
			defImage.setDef("1");
			defImgMap.put("erpMerchandiseImage", defImage);
			defImgMap.put(ErpMerchandiseImage.COL_IMAGE_URL, defImgPath);
			defImgMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandise.getMerchandiseId());
			SqlUtis.prepareSql(defImgMap, 
					SqlUtis.getSQL(defImgMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ),
					SqlUtis.getSQL(defImgMap,"a."+ErpMerchandiseImage.COL_IMAGE_URL,Types.VARCHAR,SqlUtis.EQ)
					);
			resultInfo = erpMerchandiseImageService.update(defImgMap);
			}
		}

		//更新发布任务
//		try {
//			Map publishTaskMap = new HashMap();
//			ErpPublishTask publishtask = new ErpPublishTask();
//			publishtask.setTaskStatus("11");
//			publishTaskMap.put(ErpPublishTask.COL_TASK_ID, merchandise.getMerchandiseId());
//			publishTaskMap.put(ErpPublishTask.COL_TASK_TYPE_ID, "03");
//			publishTaskMap.put("erpPublishTask",publishtask);
//			SqlUtis.prepareSql(publishTaskMap,
//					SqlUtis.getSQL(publishTaskMap,"a."+ErpPublishTask.COL_TASK_ID,Types.VARCHAR,SqlUtis.EQ),
//					SqlUtis.getSQL(publishTaskMap,"a."+ErpPublishTask.COL_TASK_TYPE_ID,Types.VARCHAR,SqlUtis.EQ)
//					);
//			this.erpPublishTaskService.update(publishTaskMap);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		return resultInfo;
	}
	
	
	/**
	 * 新增商品信息
	 * @param mapInfoMap
	 */
	@Transactional
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Integer addGoodsInfo(@ErpMerchandise.AnnoBarcode String barcode,@ErpMerchandise.AnnoShopId String shopId,@ErpMerchandise.AnnoMerchandiseName String merchandiseName,
			@ErpMerchandise.AnnoBrief String brief,@ErpMerchandise.AnnoMerchandiseTypeId String merchandiseTypeId,@ErpMerchandise.AnnoShopMerchandiseTypeId String shopMerchandiseTypeId,
			@ErpMerchandise.AnnoUnitName String unitName,@Num(length=5) String inventory,@Num(length=5,scale=2)String costPrice,@Num(length=5,scale=2)String mktprice,
			@Num(length=5,scale=2) String salePrice,@ErpMerchandise.AnnoVendorName String vendorName,String detail,@ErpMerchandise.AnnoClosed String closed,
			@ErpMerchandise.AnnoRecommended String recommended,@ErpMerchandise.AnnoVirtual String virtual,@Num(length=5)String inventoryWarn,Map mapInfoMap,@ErpMerchandise.AnnoSeoWeight String seoWeight) {
		
		Merchandise merchandise=new Merchandise();
		merchandise.setBarcode(barcode);
		merchandise.setShopId(shopId);
		merchandise.setMerchandiseName(merchandiseName);
		merchandise.setBrief(brief);
		merchandise.setMerchandiseTypeId(merchandiseTypeId);
		merchandise.setShopMerchandiseTypeId(shopMerchandiseTypeId); 
		merchandise.setUnitName(unitName);
		if(!(inventory==null || "".equals(inventory))){
			merchandise.setInventory(Integer.parseInt(inventory));
		}else{
			merchandise.setInventory(0);
		}
		if(!(costPrice==null || "".equals(costPrice))){
			merchandise.setCostPrice(Double.parseDouble(costPrice));
		}else{
			merchandise.setCostPrice(0.0);
		}
		if(!(mktprice==null || "".equals(mktprice))){
			merchandise.setMktprice(Double.parseDouble(mktprice));
		}else{
			merchandise.setMktprice(0.0);
		}
		if(StringUtils.isNotBlank(seoWeight)){
			merchandise.setSeoWeight(Double.parseDouble(seoWeight));
		}else{
			merchandise.setSeoWeight(0.0);
		}
		merchandise.setSalePrice(Double.parseDouble(salePrice));
		merchandise.setVendorName(vendorName);
		merchandise.setDetail(detail);
		merchandise.setClosed(closed);
		merchandise.setRecommended(recommended);
		merchandise.setVirtual(virtual);
		if(!(inventoryWarn==null || "".equals(inventoryWarn))){
			merchandise.setInventoryWarn(Integer.parseInt(inventoryWarn));
		}else{
			merchandise.setInventoryWarn(0);
		}
		
		
		
		mapInfoMap.put("merchandise", merchandise);
		mapInfoMap.put("shopId", shopId);
		
		List<ErpMerchandiseImage> goodsPicList = (List<ErpMerchandiseImage>) mapInfoMap.get("goodsPicList");
		ErpMerchandiseImageService erpMerchandiseImageService = (ErpMerchandiseImageService) mapInfoMap.get("erpMerchandiseImageService");
		ErpInventory erpInventory = (ErpInventory) mapInfoMap.get("erpInventory");
		ErpInventoryService erpInventoryService = (ErpInventoryService) mapInfoMap.get("erpInventoryService");
		ErpSalePrice erpSalePrice = (ErpSalePrice) mapInfoMap.get("erpSalePrice");
		ErpSalePriceService erpSalePriceService= (ErpSalePriceService) mapInfoMap.get("erpSalePriceService");
		String defImgPath = (String) mapInfoMap.get("defImgPath");
		Integer resultInfo = new Integer(1); 
		
		//新增商品基本信息
		Map goodsInfoMap = new HashMap();
		goodsInfoMap.put("erpMerchandise", merchandise);
		goodsInfoMap.put(Merchandise.COL_SHOP_ID, merchandise.getShopId());
		SqlUtis.prepareSql(goodsInfoMap,
				SqlUtis.getSQL(goodsInfoMap,"a."+Merchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				);
		String returnId = this.erpMerchandiseService.insert(goodsInfoMap).toString();
		if (returnId == null) {
			resultInfo = 0;
			return resultInfo ;
		}
		// 新增库存和警告阀值
		erpInventory = new ErpInventory();
		erpInventory.setInventory(merchandise.getInventory());
		erpInventory.setWarn(merchandise.getInventoryWarn());
		erpInventory.setMerchandiseId(returnId);
		Map InvMap = new HashMap();
		InvMap.put("erpInventory", erpInventory);
		InvMap.put(ErpInventory.COL_MERCHANDISE_ID, returnId);
		SqlUtis.prepareSql(InvMap, SqlUtis.getSQL(InvMap, "a." + ErpInventory.COL_MERCHANDISE_ID, Types.VARCHAR, SqlUtis.EQ));
		String invId = erpInventoryService.insert(InvMap).toString();
		if(invId==null){
			resultInfo = 0;
			return resultInfo ;
		}
		// 新增商品销售价和商品成本价
		erpSalePrice = new ErpSalePrice();
		erpSalePrice.setPrice(merchandise.getSalePrice());
		erpSalePrice.setCost(merchandise.getCostPrice());
		erpSalePrice.setMerchandiseId(returnId);
		Map priceMap = new HashMap();
		priceMap.put("erpSalePrice", erpSalePrice);
		priceMap.put(ErpSalePrice.COL_MERCHANDISE_ID, returnId);
		SqlUtis.prepareSql(priceMap, SqlUtis.getSQL(priceMap, "a." + ErpSalePrice.COL_MERCHANDISE_ID, Types.VARCHAR, SqlUtis.EQ));
		String priId = erpSalePriceService.insert(priceMap).toString();
		if(priId==null){
			resultInfo = 0;
			return resultInfo ;
		}
		// 新增商品图片
		if (goodsPicList != null && goodsPicList.size() > 0) {
			if(StringUtils.isBlank(defImgPath)){
				defImgPath = goodsPicList.get(0).getImageUrl();
			}
			for (ErpMerchandiseImage erpMerchandiseImage : goodsPicList) {
				Map picMap = new HashMap();
				erpMerchandiseImage.setGoodsId(returnId);
				if (erpMerchandiseImage.getImageUrl().equals(defImgPath)) {
					erpMerchandiseImage.setDef("1");
				}
				picMap.put("erpMerchandiseImage", erpMerchandiseImage);
				picMap.put(ErpMerchandiseImage.COL_GOODS_ID, returnId);
				SqlUtis.prepareSql(picMap, SqlUtis.getSQL(picMap, "a." + ErpMerchandiseImage.COL_GOODS_ID, Types.VARCHAR, SqlUtis.EQ));
				String imaId = erpMerchandiseImageService.insert(picMap).toString();
				if(imaId==null){
					resultInfo = 0;
					return resultInfo ;
				}
			}
		}
		//新增发布任务
		try {
			ErpPublishTask publishtask = new ErpPublishTask();
			publishtask.setTaskId(returnId);
			publishtask.setTaskType("03");
			publishtask.setTaskTypeId("03");
			publishtask.setMiscTx(merchandise.getShopId());
			publishtask.setTaskStatus("11");
			publishtask.setTaskLevel(0.0);
			publishtask.setLastUpdOprId(mapInfoMap.get("userId").toString());
			publishtask.setLastUpdTxnId("0000");
			publishtask.setDelFlag("1");
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
			publishtask.setLastUpdTs(format.format(new Date()));
			
			this.erpPublishTaskService.insert(publishtask);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resultInfo ;
	}
	/**
	 * 删除图片
	 */
	@Transactional
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Integer delPic(@Security String imageId, ErpMerchandiseImageService erpMerchandiseImageService){
		
		
		Map mapInfo = new HashMap();
		mapInfo.put(ErpMerchandiseImage.COL_IMAGE_ID,imageId);
		mapInfo.put("erpMerchandiseImageService", this.erpMerchandiseImageService);
		
		
		Map delMap = new HashMap();
		delMap.put(ErpMerchandiseImage.COL_IMAGE_ID, mapInfo.get(ErpMerchandiseImage.COL_IMAGE_ID));
		//delMap.put(ErpMerchandiseImage.COL_GOODS_ID, mapInfo.get(ErpMerchandiseImage.COL_GOODS_ID));
		SqlUtis.prepareSql(delMap
				, SqlUtis.getSQL(delMap,"a."+ErpMerchandiseImage.COL_IMAGE_ID,Types.VARCHAR,SqlUtis.EQ),//条形码
				SqlUtis.getSQL(delMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ)//商品ID
				);
		/*ErpMerchandiseImage defImage = erpMerchandiseImageService.one(delMap);
		Integer resultDel ; 
		if(defImage.getDef().equals("1")){
			resultDel=-1;
		}else{
			resultDel = erpMerchandiseImageService.delete(delMap);
		}*/

		return erpMerchandiseImageService.delete(delMap);
	}
	
	
	
	/**
	 * 获得商品图片
	 * @return
	 */
	public ErpMerchandiseImage getMerchandiseImage(@ErpMerchandise.AnnoMerchandiseId String merchandiseId){
		HashMap goodPicMap = new HashMap();
		goodPicMap.put(ErpMerchandiseImage.COL_GOODS_ID, merchandiseId);
		goodPicMap.put(ErpMerchandiseImage.COL_DEF, "1");
		SqlUtis.prepareSql(goodPicMap
				, SqlUtis.getSQL(goodPicMap,"a."+ErpMerchandiseImage.COL_GOODS_ID,Types.VARCHAR,SqlUtis.EQ),
				SqlUtis.getSQL(goodPicMap,"a."+ErpMerchandiseImage.COL_DEF,Types.VARCHAR,SqlUtis.EQ)//商家ID
				);
		
		 return this.erpMerchandiseImageService.one(goodPicMap);
		
	}
	
	public Merchandise erpMerchandiseDetail(@ErpMerchandise.AnnoShopId String shopId,@ErpMerchandise.AnnoMerchandiseId String merchandiseId){
		
		Map formMap=new HashMap<String, String>();
		if(merchandiseId!=null&&merchandiseId!=""){
			formMap.put(Merchandise.COL_MERCHANDISE_ID, merchandiseId);
		}
		formMap.put(Merchandise.COL_SHOP_ID, shopId);
		SqlUtis.prepareSql(formMap
				, SqlUtis.getSQL(formMap,"a."+Merchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ),//商家ID
				SqlUtis.getSQL(formMap,"a."+Merchandise.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)//商品id
				);
		
		//request.setAttribute("erpMerchandiseImage",erpMerchandiseImage);
		return this.one(formMap);
	}
	
	/**
	 * 获取商品分类列表
	 * @param shopId
	 * @param merchandiseTypeId
	 * @param merchandiseTypeName
	 * @param page
	 * @return
	 */
	public List<MerchandiseType> getMerchandiseTypeList(@ErpMerchandiseType.AnnoShopId String shopId,@ErpMerchandiseType.AnnoMerchandiseTypeId String merchandiseTypeId
			,@ErpMerchandiseType.AnnoMerchandiseTypeName String merchandiseTypeName){
		
		Map mapInfo = new HashMap();
		 
		mapInfo.put(MerchandiseType.COL_SHOP_ID,shopId);
		
		mapInfo.put(MerchandiseType.COL_MERCHANDISE_TYPE_ID,merchandiseTypeId);
		
		mapInfo.put(MerchandiseType.COL_MERCHANDISE_TYPE_NAME,merchandiseTypeName);
		
		SqlUtis.prepareSql(mapInfo
				, SqlUtis.getSQL(mapInfo,"a."+MerchandiseType.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ)
				, SqlUtis.getSQL(mapInfo,"a."+MerchandiseType.COL_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ)
				, SqlUtis.getSQL(mapInfo,"a."+MerchandiseType.COL_MERCHANDISE_TYPE_NAME,Types.VARCHAR,SqlUtis.LIKE)
				);
		
				
		return this.list(this.getStatementName("getMerchandiseTypeList"),mapInfo);
	}
    
	/**
	 * 添加商品分类
	 * @param parentId
	 * @param merchandiseTypeName
	 * @param shopId
	 */
	@Transactional
	public Object insertMerchandiseType(@ErpMerchandiseType.AnnoParentId String parentId,@ErpMerchandiseType.AnnoMerchandiseTypeName String merchandiseTypeName
			,@ErpMerchandiseType.AnnoShopId String shopId){
		
		ErpMerchandiseType merchandiseType=new ErpMerchandiseType();
		
		merchandiseType.setMerchandiseTypeName(merchandiseTypeName);
		
		merchandiseType.setParentId(parentId);
		
		merchandiseType.setShopId(shopId);
		
		return this.erpMerchandiseTypeService.insert(merchandiseType);
	}
	
	
	/**
	 * 更新商品分类
	 * @param merchandiseTypeId
	 * @param parentId
	 * @param merchandiseTypeName
	 * @param shopId
	 */
	@Transactional
	public void updateMerchandiseType(@ErpMerchandiseType.AnnoMerchandiseTypeId String merchandiseTypeId,@ErpMerchandiseType.AnnoParentId String parentId
			,@ErpMerchandiseType.AnnoMerchandiseTypeName String merchandiseTypeName,@ErpMerchandiseType.AnnoShopId String shopId){
		
		Map mapInfo = new HashMap();
		
		mapInfo.put(ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,merchandiseTypeId);
		
		SqlUtis.prepareSql(mapInfo
				, SqlUtis.getSQL(mapInfo,"a."+ErpMerchandiseType.COL_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ)
				);
				
		ErpMerchandiseType merchandiseType=this.erpMerchandiseTypeService.one(mapInfo);
		
		merchandiseType.setMerchandiseTypeName(merchandiseTypeName);
		
		merchandiseType.setParentId(parentId);
		
		merchandiseType.setShopId(shopId);
		
		this.erpMerchandiseTypeService.update(merchandiseType);
	}
	
	/**
	 * 根据商品分类ID检测该分类是否被商品关联
	 * @param merchandiseTypeId
	 * @return
	 */
	public boolean checkMerchandiseType(@ErpMerchandise.AnnoMerchandiseTypeId String merchandiseTypeId){
		
		Map map2=new HashMap();
		map2.put(ErpMerchandise.COL_SHOP_MERCHANDISE_TYPE_ID, merchandiseTypeId);
		
		SqlUtis.prepareSql(map2, 
				SqlUtis.getSQL(map2, "a."+ErpMerchandise.COL_SHOP_MERCHANDISE_TYPE_ID,Types.VARCHAR,SqlUtis.EQ));
		
		List<ErpMerchandise> list2=this.erpMerchandiseService.list(map2);
		
		
		if(list2.size()>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	
	/**
	 * 根据商品分类ID检测该分类是否被分类关联（是否是某个分类的父级分类）
	 * @param merchandiseTypeId
	 * @return
	 */
	public boolean checkMerchandiseTypeParent(@ErpMerchandiseType.AnnoParentId String merchandiseTypeId){
		
		Map map=new HashMap();
		map.put(ErpMerchandiseType.COL_PARENT_ID, merchandiseTypeId);
		
		SqlUtis.prepareSql(map, 
				SqlUtis.getSQL(map, "a."+ErpMerchandiseType.COL_PARENT_ID,Types.VARCHAR,SqlUtis.EQ));
		
		List<ErpMerchandise> list=this.erpMerchandiseTypeService.list(map);
		if(list.size()>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	/**
	 * 删除商品分类
	 * @param merchandiseTypeId
	 * @return
	 */
	public boolean deleteMerchandiseType(@ErpMerchandiseType.AnnoMerchandiseTypeId String merchandiseTypeId){
		
		
		int num=this.erpMerchandiseTypeService.delete(merchandiseTypeId);
		if(num>0){
			return true;
		}else{
			return false;
		}
		
	}
	
	/**
	 * 检查条形码是否存在
	 * @param barcode
	 * @return
	 */
	public boolean checkBarcode(@ErpMerchandise.AnnoBarcode String barcode,@ErpMerchandise.AnnoShopId String shopId){
		Map map=new HashMap();
		map.put(ErpMerchandise.COL_BARCODE, barcode);
		map.put(ErpMerchandise.COL_SHOP_ID, shopId);
		
		SqlUtis.prepareSql(map, SqlUtis.getSQL(map, "a."+ErpMerchandise.COL_BARCODE,Types.VARCHAR,SqlUtis.EQ),
				SqlUtis.getSQL(map, "a."+ErpMerchandise.COL_SHOP_ID,Types.VARCHAR,SqlUtis.EQ));
		
		return this.erpMerchandiseService.exists(map);
		
	}
	
	/**
	 * 批量上下架
	 * @param checkbox_merchandise
	 * @param closed
	 */
	public void batchUpdateCloseService(List<String> checkbox_merchandise,String closed){
		
		
		for(int i=0;i<checkbox_merchandise.size();i++){
			
			ErpMerchandise erpMerchandise=this.erpMerchandiseService.one(checkbox_merchandise.get(i));
			erpMerchandise.setClosed(closed);
			this.erpMerchandiseService.update(erpMerchandise);
			
		}
		
		
	}
	
	/**
	 * 批量推荐/批量取消推荐
	 * @param checkbox_merchandise
	 * @param recommend
	 */
	public void batchUpdateRecommendService(List<String> checkbox_merchandise,String recommend){
		
		
		for(int i=0;i<checkbox_merchandise.size();i++){
			
			ErpMerchandise erpMerchandise=this.erpMerchandiseService.one(checkbox_merchandise.get(i));
			erpMerchandise.setRecommended(recommend);
			this.erpMerchandiseService.update(erpMerchandise);
			
		}
		
		
	}
	
	/**
	 * 批量推荐/批量取消推荐
	 * @param checkbox_merchandise
	 * @param recommend
	 */
	public void updateMerchandiseFieldService(String merchandiseId,String merchandiseName,String mktprice,String inventory,String salePrice){
		
			ErpMerchandise erpMerchandise=this.erpMerchandiseService.one(merchandiseId);
			
			if(!StringUtils.isBlank(merchandiseName)){
				erpMerchandise.setMerchandiseName(merchandiseName);
				this.erpMerchandiseService.update(erpMerchandise);
			}
			if(!StringUtils.isBlank(mktprice)){
				erpMerchandise.setMktprice(Double.parseDouble(mktprice));
				this.erpMerchandiseService.update(erpMerchandise);
			}
			
			if(!StringUtils.isBlank(inventory)){
				
				ErpInventory erpInventory =new ErpInventory();
				erpInventory.setInventory(Integer.valueOf(inventory));
				Map InvMap = new HashMap();
				InvMap.put("erpInventory", erpInventory);
				InvMap.put(ErpInventory.COL_MERCHANDISE_ID, merchandiseId);
				SqlUtis.prepareSql(InvMap,
						SqlUtis.getSQL(InvMap,"a."+ErpInventory.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
						);
				erpInventoryService.update(InvMap);
				Map merMap = new HashMap();
				merMap.put(ErpMerchandise.COL_MERCHANDISE_ID, merchandiseId);
				SqlUtis.prepareSql(merMap,
						SqlUtis.getSQL(merMap,"a."+ErpMerchandise.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
						);
				erpMerchandiseService.update(merMap);
			}
			
			if(!StringUtils.isBlank(salePrice)){
				ErpSalePrice erpSalePrice = new ErpSalePrice();
				erpSalePrice.setPrice(Double.parseDouble(salePrice));
				Map priceMap = new HashMap();
				priceMap.put("erpSalePrice", erpSalePrice);
				priceMap.put(ErpSalePrice.COL_MERCHANDISE_ID,merchandiseId);
				SqlUtis.prepareSql(priceMap,
						SqlUtis.getSQL(priceMap,"a."+ErpSalePrice.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
						);
				erpSalePriceService.update(priceMap);
				Map merMap = new HashMap();
				merMap.put(ErpMerchandise.COL_MERCHANDISE_ID, merchandiseId);
				SqlUtis.prepareSql(merMap,
						SqlUtis.getSQL(merMap,"a."+ErpMerchandise.COL_MERCHANDISE_ID,Types.VARCHAR,SqlUtis.EQ)
						);
				erpMerchandiseService.update(merMap);
			}
		
		
	}


	
	
}
